/**
* @file				adc_config.c
* @author  		Piyumal Ranawaka st20000427
* @version 		0.1
* @date    		November 15, 2014
* @brief   		Contains the necessary functions to initialize humidity sensor and to use it for the Weather Monitoring System
*/



/* Includes ------------------------------------------------------------------*/
#include "humidity.h"

/* Private types ------------------------------------------------------------------*/
/* Private constants --------------------------------------------------------------*/
#define GAIN=0.02442002442   // (GAIN=100/4095)Caliberation factor to map ADC register count with exact Humidity value % RH.  


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

private uint16_t adc_result; 			//final result of ADC conversion or register value
private uint16_t humidity_value; 	// final value of humidity in %RH



/**
* @brief    Initialize Humidity Sensor
* @param    None
* @retval   None
*/

void humidity_config(void){

	RCC_Configuration_Adc1();	//sub method to configure RCC 
	GPIO_Configuration_Adc1();//sub method to configure GPIO
	ADC1_Configuration();			//sub method to configure ADC features
}



/**
* @brief    Configure RCC for humidity sensor
* @param    None
* @retval   None
*/

void RCC_Configuration_Adc1(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}




/**
* @brief    Configure GPIO for humidity sensor
* @param    None
* @retval   None
*/
void GPIO_Configuration_Adc1(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_StructInit(&GPIO_InitStructure);

	//for ADC1 on Port C pin 1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
}


/**
* @brief    Configure ADC features for humidity sensor
* @param    None
* @retval   None
*/

void ADC1_Configuration(void)
{
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	ADC_InitTypeDef       ADC_InitStructure;

	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
	ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
	ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

	ADC_CommonInit(&ADC_CommonInitStructure);

	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfConversion = 1;


	ADC_StructInit(&ADC_InitStructure);

	ADC_Init(ADC1, &ADC_InitStructure);

	ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_480Cycles);
	ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);

	ADC_Cmd(ADC1, ENABLE);    //The ADC is powered on by setting the ADON bit in the ADC_CR2 register.
	//When the ADON bit is set for the first time, it wakes up the ADC from the Power-down mode.

}



/**
* @brief    Method for reading humidity
* @param    None
* @retval   humidity_value:final humidity value in %RH
*/
uint16_t humidity_read(void)
{
			ADC_SoftwareStartConv(ADC1);
			while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
			adc_result= ADC_GetConversionValue(ADC1);
			humidity_value=adc_result*GAIN;	// convert register count into humidity value.
			return humidity_value;

}


